package com.baomidou.springboot.controller;

import com.baomidou.springboot.model.ImageVo;
import com.baomidou.springboot.service.FileService;
import com.baomidou.springboot.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/file")
public class FileController {

    /* 图片存放路径 */
    private String imgPath = "D:/sinosun/images/";
    /* 图片访问路径 */
    private String requestImgPath = "http://10.206.1.81/images/";
    /* 文件存放路径 */
    private String filePath = "D:/sinosun/file/";
    /* 文件访问路径 */
    private String requestFilePath = "http://10.206.1.81/file/";

    @Autowired
    private FileService fileService;


    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(MultipartFile file) {
        FileOutputStream fos = null;
        File filePathDir = new File(filePath);
        if (!filePathDir.exists()) {
            filePathDir.mkdirs();
        }
        String fileName = StringUtil.getUUid() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String path = filePath + fileName;
        File f = new File(path);
        try {
            fos = new FileOutputStream(f);
            fos.write(file.getBytes(), 0, file.getBytes().length);
        } catch (Exception e) {
            return "";
        } finally {
        }
        return requestFilePath + fileName;
    }

    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public ImageVo uploadImg(MultipartFile file) {
        {
            FileOutputStream fos = null;
            File filePathDir = new File(filePath);
            if (!filePathDir.exists()) {
                filePathDir.mkdirs();
            }
            String imgName = StringUtil.getUUid() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            File f = new File(imgPath + imgName);
            ImageVo imageVo = new ImageVo();
            imageVo.setErrno(0);
            List<String> urlList = new ArrayList<>();
            urlList.add(requestImgPath + imgName);
            imageVo.setData(urlList);
            try {
                fos = new FileOutputStream(f);
                fos.write(file.getBytes(), 0, file.getBytes().length);
            } catch (Exception e) {
                return null;
            } finally {
            }
            return imageVo;
        }
    }

    @RequestMapping("/download")
    public void download(HttpServletResponse response, String id) {
        // 设置头
        ServletOutputStream out;
        com.baomidou.springboot.entity.File f = fileService.findById(id);
        String fileName = f.getPath();
        if (null == f.getPath()) {
            return;
        }
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName.substring(requestFilePath.length()));
        // 资源文件
        File file = new File(filePath + fileName.substring(requestFilePath.length()));
        try {
            FileInputStream fis = new FileInputStream(file);
            out = response.getOutputStream();

            int b = 0;
            byte[] array = new byte[1024 * 1024];
            while ((b = fis.read(array)) != -1) {
                out.write(array, 0, b);
            }
            out.close();
            out.flush();
            fis.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
